package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.R;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.PaySDK;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.PaySDKFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class GoPayAppActivity extends AppCompatActivity {

    private Map<String, Object> message = null;
    private long startTime, endTime;
    private PaySDK paySDK = null;
    private static boolean isFirst = true;

    //public static String PATH = "https://omnipay.cib.cibfintech.com/appserver/service/GenOrderService.r";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.go_pay_app_activity);
        paySDK = PaySDKFactory.getInstance(this);
        startTime = System.currentTimeMillis();
        Intent intent = getIntent();
        //String packageName = intent.getStringExtra("packageName");
        intent.setComponent(new ComponentName("cc.rengu.android.pay.ipps", "cc.rengu.android.ui.activity.CollectionMoney"));
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", (Serializable) intent.getExtras().get("data"));
        intent.putExtras(bundle);
        try {
            startActivityForResult(intent, PaySDK.REQ_PAY);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "没有支付APP，请下载安装", Toast.LENGTH_SHORT).show();
            this.finish();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        isFirst = isFirst == true ? false : true;
        if (isFirst) {
            Log.e("PaySdkActivity", "onResume()方法 ");
            endTime = System.currentTimeMillis();
            if ((endTime - startTime) <= 600 * 1000 && message != null) {
                paySDK.sendSuccess(message);
                this.finish();
            } else {
                if ((endTime - startTime) > 600 * 1000) {
                    //paySDK.sendFail("交易超时,请重新支付");
                } else {
                    //paySDK.sendFail("交易失败");
                }
                this.finish();
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("cellback","--------Huidao --");

        if (requestCode == PaySDK.REQ_PAY && resultCode == RESULT_OK) {
            HashMap<String, Object> info = null;
            if (data != null) {
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    info= (HashMap<String, Object>) bundle.get("result");
                    String resCode = (String) info.get("resCode");
                    if(resCode.equals("OK")){
                        //sendMessageToService();
                    }else {
                      Toast.makeText(GoPayAppActivity.this,"支付失败",Toast.LENGTH_SHORT).show();
                    }

                    finish();
                }
            }

        }

    }

    private void sendMessageToService() {
        HttpParams params = new HttpParams();
        params.put("","");

        OkGo.<String>post("").params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

            }
        });
    }

    //返回键不可用
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        } else {
            return false;
        }
    }
}
